﻿using HDS.Common.Helper;
using HDS.Model.Entity.Log;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace HDS.Data
{
    public class LogDbContext
    {
        public SqlSugarScope Db;

        public LogDbContext(string configId = "Log")
        {
            Db = new SqlSugarScope(new ConnectionConfig()
            {
                ConfigId = configId,
                ConnectionString = AppSettings.GetAppSettingValue(new string[] { "ConnectionStrings", configId }),
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                //IsShardSameThread = false,
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, pars) =>
                    {
#if DEBUG
                        string keys = "----------------------------------" + System.Environment.NewLine + "【SQL参数】：" + System.Environment.NewLine;
                        foreach (var param in pars)
                        {
                            keys += $"{param.ParameterName} : {param.Value}      ";
                        }
                        Console.WriteLine(keys + System.Environment.NewLine + "【SQL语句】" + System.Environment.NewLine + sql + System.Environment.NewLine);
#endif
                    }
                },
                MoreSettings = new ConnMoreSettings()
                {
                    //IsAutoRemoveDataCache = true,
                    DisableNvarchar = true
                },
                InitKeyType = InitKeyType.Attribute
            });
        }

        public ISimpleClient<UpdateLog> UpdateLog => Db.GetSimpleClient<UpdateLog>();
    }
}
